<template>
  <div class="login">
    <div class="tittle" @click="login">
      登 录<br />
      我在Login组件中，router-view在app中
    </div>

    <div class="loginForm">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        label-position="left"
        class="demo-ruleForm"
        size="small"
      >
        <el-form-item label="用户名" prop="usrname">
          <el-input v-model.trim="ruleForm.usrname" :prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input
            type="password"
            v-model.trim="ruleForm.password"
            autocomplete="off"
            :prefix-icon="Key"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model.trim="ruleForm.checkPass"
            autocomplete="off"
            :prefix-icon="Key"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="年龄" prop="age">
          <el-input v-model.number="ruleForm.age"></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { Key, User } from "@element-plus/icons";
import { shallowRef } from "vue";
import axios from 'axios';
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入账户"));
      }else {
        callback()
      }
    };
    // var checkAge = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("年龄不能为空"));
    //   }
    //   setTimeout(() => {
    //     if (!Number.isInteger(value)) {
    //       callback(new Error("请输入数字值"));
    //     } else {
    //       if (value < 18) {
    //         callback(new Error("必须年满18岁"));
    //       } else {
    //         callback();
    //       }
    //     }
    //   }, 1000);
    // };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        usrname: "",
        password: "",
        checkPass: "",
        // age: "",
      },
      rules: {
        usrname: [{ validator: checkName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        // age: [{ validator: checkAge, trigger: "blur" }],
      },
      Key: shallowRef(Key),
      User: shallowRef(User),
    };
  },
  methods: {
    login() {
      console.log("当前环境变量：" + process.env.NODE_ENV);
      this.$router.push("./noticeboard");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("/login",this.ruleForm).then((res) => {
            if(res.data.code == '200'){
              this.$router.push("./noticeboard");
              ElMessage.success('登录成功！')
            }else{
              alert("登录失败！")
            }
          }).catch(() => {
            alert("登录失败！")
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  overflow: hidden;
  // border: solid 1px #666;
  // background: #888;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
.tittle {
  margin-bottom: 20px;
  text-align: center;
  font-size: 30px;
}
.loginForm {
  width: 550px;
  height: 300px;
  border-radius: 15px;
  background: #a79292;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
